package com.gitee.feizns.quickstart.easyexcel.impl;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.gitee.feizns.quickstart.easyexcel.Sheet;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * excel 读取监听器默认实现
 * @see AnalysisEventListener
 * @author feizns
 * @since 2023/2/1
 */
@Getter
public class SheetReaderListener extends AnalysisEventListener<Map<Object, Object>> {

    /**
     * 表头
     */
    private final List<String> titles = new ArrayList<>();

    /**
     * 数据
     */
    private final List<List<Object>> data = new ArrayList<>();

    @Override
    public void invoke(Map<Object, Object> data, AnalysisContext context) {
        List<Object> list = new ArrayList<>();
        data.forEach((k, v) -> list.add(v));
        this.data.add(list);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        headMap.forEach((k, v) -> titles.add(v));
    }

    /**
     * 获取工作表
     * @return {@link Sheet }
     */
    public Sheet getSheet() {
        return new Sheet(titles, data);
    }

}
